

clear
set more off
capture log close
set seed 12345
set matsize 5000
set maxvar 20000	

* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"


*********
**  Prep alt treatment variables
*********
* treatment and mtr's from sample excluding cap gains and qual dividends + bus (nbus)
local ivsamp "nbus"
use "$DATA/scorp_tc_`ivsamp'_firm_file.dta", clear
des
drop mtr_firm mtr_2013_firm t1_mtr t2_mtr atr* liab*

foreach v in t1 mtr mtr_2013  {
	ren `v' `v'_`ivsamp'
}

* Change in MTR IV
foreach p in mtr_2013_`ivsamp' mtr_`ivsamp' {
	gen ln`p'=ln(`p') if `p' > 0
	replace ln`p'=ln(`p'+((`p'^2)+1)^.5) if `p'<=0
}
gen dlnmtr_iv_`ivsamp'`ivavg' = lnmtr_2013_`ivsamp'`ivavg' - lnmtr_`ivsamp'`ivavg'
replace dlnmtr_iv_`ivsamp'`ivavg' = 0 if dlnmtr_iv_`ivsamp'`ivavg'<0

foreach n in dlnmtr_iv_`ivsamp' t1_`ivsamp' {
	gen zz = 0
	replace zz = `n' if year==2012
	bys firm_tin: egen `n'_2012=sum(zz)
	drop zz
}
foreach n in t1_`ivsamp' {
	gen zz = 0
	replace zz = `n' if year==2011
	bys firm_tin: egen `n'_2011=sum(zz)
	drop zz
}

keep firm_tin year *mtr* t1_* 
sort firm_tin year
tempfile t`ivsamp'
save `t`ivsamp'', replace


* mtr's from sample excluding cap gains and qual dividends [based on avg 2011-2012]
local ivsamp "ncg"
local ivavg "_avg"
use "$DATA/scorp_tc_`ivsamp'_firm_file`ivavg'.dta", clear
des
drop mtr_firm mtr_2013_firm t1_mtr t2_mtr atr* liab*
foreach v in t1 t2 mtr mtr_2013 {
	ren `v' `v'_`ivsamp'`ivavg'
}

* Change in MTR IV
foreach p in mtr_2013_`ivsamp'`ivavg' mtr_`ivsamp'`ivavg' {
	gen ln`p'=ln(`p') if `p' > 0
	replace ln`p'=ln(`p'+((`p'^2)+1)^.5) if `p'<=0
}
gen dlnmtr_iv_`ivsamp'`ivavg' = lnmtr_2013_`ivsamp'`ivavg' - lnmtr_`ivsamp'`ivavg'
replace dlnmtr_iv_`ivsamp'`ivavg' = 0 if dlnmtr_iv_`ivsamp'`ivavg'<0

foreach n in dlnmtr_iv_`ivsamp'`ivavg' t1_`ivsamp'`ivavg'  {
	gen zz = 0
	replace zz = `n' if year==2012
	bys firm_tin: egen `n'_2012=sum(zz)
	drop zz
}

foreach n in t1_`ivsamp'`ivavg'  {
	gen zz = 0
	replace zz = `n' if year==2011
	bys firm_tin: egen `n'_2011=sum(zz)
	drop zz
}

keep firm_tin year *mtr* t1_* 
sort firm_tin year
tempfile t`ivsamp'`ivavg'
save `t`ivsamp'`ivavg'', replace

  

**********
**  load main file and merge on
**********
use $DATA/reg_file_firm, clear

forval y = `yrb' / `yre' {	
	gen th`y' = 0
}

sort firm_tin year
merge 1:1  firm_tin year using `tnbus'
tab _merge bal_liab
drop _merge

sort firm_tin year
merge 1:1  firm_tin year using `tncg_avg'
tab _merge bal_liab
drop _merge


*******************************************
** regressions
*******************************************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1

local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year"
local r2 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year"
local r3 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year"
local r4 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"


****************************
****************************
****************************
** Robustness 
	* Figure A.1
	* Figure A.2, panel B
	* Figure A.5
	* Table A.6
	* Table A.7
****************************
****************************
****************************

*********************************************************************************************
*********************************************************************************************
**  Robustness - different base year treatment exposure [Fig A.1, Table A.6]
*********************************************************************************************
*********************************************************************************************
********************
**  Base 2012
********************
local t "t1_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}

local events "t`yrb'-t2011 t`bp1'-t`yre' y`yrb'-y2011 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/figure_A1_panelA_itt.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/table_A6_base2012_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "t1_2012"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0) 
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2011 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2011 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure_A1_panelA_2sls.csv", replace se ar2 mtitles( ) keep(th20*) addnote("Each specification includes a different set of control variabels")
esttab using "$OUTPUT/table_A6_base2012_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "t1_2012"

local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}

local events "t`yrb'-t2011 t`bp1'-t`yre' y`yrb'-y2011 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_base2012_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}


****************************
**  treat based on 2010-2012
****************************
foreach v in t1 {
	gen vv = .
	replace vv = `v' if year==2010
	bys firm_tin: egen `v'_2010 = max(vv)
	drop vv
}

gen tt1012=.
replace tt1012 = 1 if t1_2010==1 & t1_2011==1 & t1_2012==1
replace tt1012 = 0 if t1_2010==0 & t1_2011==0 & t1_2012==0

local t "tt1012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}

local events "t`yrb'-t2009 t`bp1'-t`yre' y`yrb'-y2009 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/figure_A1_panelB_itt.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/table_A6_base1012_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1012"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0) 
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2013 th2014 th2015 th2016 y2008 y2009 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure_A1_panelB_2sls.csv", replace se ar2 mtitles( ) keep(th20*) addnote("Each specification includes a different set of control variabels")
esttab using "$OUTPUT/table_A6_base1012_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********  
local trt "tt1012_ncg"

local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}

local events "t`yrb'-t2009 t`bp1'-t`yre' y`yrb'-y2009 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in  4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_base1012_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



*********************************************************************************************
*********************************************************************************************
**  Robustness - excl top 0.1% - ncg [Fig A.2, panel B, Table A.6]
*********************************************************************************************
*********************************************************************************************
replace top01_all = 0 if top01==0
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & top01_all==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/figure_A2_panelB_top01.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/table_A6_top01_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1112"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)  & top01_all==0
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016) & top01_all==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_top01_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112_ncg"

local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in  4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016) & top01_all==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_top01_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}




*********************************************************************************************
*********************************************************************************************
**  Robustness - wage income is largest other source - ncg [Fig A.2, panel B, Table A.6]
*********************************************************************************************
*********************************************************************************************
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) &  max_nord_wage_amt==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/figure_A2_panelB_wagemax.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/table_A6_wagemax_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1112"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)  &  max_nord_wage_amt==1
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016) &  max_nord_wage_amt==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_wagemax_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112_ncg"
local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016) &  max_nord_wage_amt==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_wagemax_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



*********************************************************************************************
*********************************************************************************************
**  Robustness - capital income is not majorty share - ncg [Fig A.2, panel B, Table A.6]
*********************************************************************************************
*********************************************************************************************

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach ci in capinc2 invest2 {
	foreach o in wages_ft {
		* loop over regressions
		foreach r in 4 {
			eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & max_`ci'==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
		}
		esttab using "$OUTPUT/figure_A2_panelB_`ci'max.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
		esttab using "$OUTPUT/table_A6_`ci'max_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
		eststo clear
	}
}


*********
**  2sls main 
*********

foreach ci in capinc2 invest2 {
	local tt "tt1112"
	local tout "t1"
	forval y=2008/2016 {
		quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)  & max_`ci'==0
		predict hat`y',xb
	}
	forval y = `yrb' / `yre' {
		drop th`y'
		gen th`y' = 0
		replace th`y' = . if hat`y'==.
		replace th`y' = hat`y' if year==`y'
	}

	foreach r in 4 {
		eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016) &  max_`ci'==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_`ci'max_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
	drop hat*
}

**********
**  Elasticity
**********
local trt "tt1112_ncg"

local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach ci in capinc2 invest2 {
	foreach o in wages_ft {
		* loop over regressions
		foreach r in 4 {
			eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0)  & (year==2011 | year==2012 | year==2016) &  max_`ci'==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
		}
		esttab using "$OUTPUT/table_A6_`ci'max_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
		eststo clear
	}
}



***************************************************************************************
***************************************************************************************
**  Robustness - top net of KG and QD [Table A.6]
***************************************************************************************
***************************************************************************************

local t "tt1112_ncg"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_topncg_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}


*********
**  2sls main 
*********
local tt "tt1112_ncg"
local tout "top"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0) 
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_topncg_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*


**********
**  Elasticity
**********
local trt "tt1112_ncg"
local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in  4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_topncg_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}




***************************************************************************************
***************************************************************************************
**  Robustness - top net of business income [Table A.6]
***************************************************************************************
***************************************************************************************

gen tt1112_nbus=.
replace tt1112_nbus = 1 if t1_nbus_2011==1 & t1_nbus_2012==1
replace tt1112_nbus = 0 if t1_nbus_2011==0 & t1_nbus_2012==0
replace tt1112_nbus = . if tt1112==1 & tt1112_nbus==0
replace tt1112_nbus = . if tt1112==0 & tt1112_nbus==1

local t "tt1112_nbus"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_topnbus_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1112_nbus"
local tout "t1_nbus"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)  
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_topnbus_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112_nbus"
local t "dlnmtr_iv_nbus_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_topnbus_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}




*********************************************************************************************
*********************************************************************************************
**  Robustness - top net cap gains, avg 2011-2012 [Table A.6]
*********************************************************************************************
*********************************************************************************************

gen tt1112_avg=.
replace tt1112_avg = 1 if t1_ncg_avg_2011==1 & t1_ncg_avg_2012==1
replace tt1112_avg = 0 if t1_ncg_avg_2011==0 & t1_ncg_avg_2012==0
replace tt1112_avg = . if tt1112==1 & tt1112_avg==0
replace tt1112_avg = . if tt1112==0 & tt1112_avg==1

local t "tt1112_avg"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_avg_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1112_avg"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)  
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_avg_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112_avg"
local t "dlnmtr_iv_ncg_avg_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in  4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_avg_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}




*********************************************************************************************
*********************************************************************************************
**  Robustness - unweighted [Table A.6]
*********************************************************************************************
*********************************************************************************************
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in  wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  if (`t'==1 | `t'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_unweighted_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1112"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0) 
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_unweighted_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112"
local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in  wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  if (`trt'==1 | `trt'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_unweighted_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}




*********************************************************************************************
*********************************************************************************************
**  Robustness - excluding finance and realestate (FIRE) [Table A.6]
*********************************************************************************************
*********************************************************************************************
gen nofin = 1
replace nofin = 0 if naics_2012==52 | naics_2012==53 | naics_2012==55

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016) & nofin==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_nofire_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "tt1112"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)  & nofin==1
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0) & (year==2011 | year==2012 | year==2016)  & nofin==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_nofire_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112"
local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016)  & nofin==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_nofire_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



*************************************************************************
*************************************************************************
**  Robustness - control firms over 300K [Fig. A.5]
*************************************************************************
*************************************************************************
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if ptop3==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}		
	esttab using "$OUTPUT/figure_A5_top_v_top3.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



*********************************************************************************************
*********************************************************************************************
**  Robust -  different functional form of baseline net income controls - ncg  [Table A.7]
*********************************************************************************************
*********************************************************************************************

foreach v in netinc {
	gen z11 = .
	replace z11 = `v' if year==2011
	bys firm_tin: egen `v'_2011 = max(z11)
	drop z11
}

* avg variables for 2011 and 2012
local ags "netinc"
foreach a in `ags' {
	gen `a'_avg = (`a'_2012+`a'_2011)/2
}

/* Create deciles of production variables and owner income variables */
foreach i in netinc_2011 netinc_avg {
	xtile `i'cat = `i',nquantiles(10)
}
foreach i in netinc_avg {
	xtile `i'cat20 = `i',nquantiles(20)
}

* create splines in baseline net income
mkspline nispline 10 = netinc_2012, pctile displayknots
mkspline nispline_avg 10 = netinc_avg, pctile displayknots

* splines
local splines1 "c.nispline1##i.year c.nispline2##i.year c.nispline3##i.year c.nispline4##i.year c.nispline5##i.year c.nispline6##i.year c.nispline7##i.year c.nispline8##i.year c.nispline9##i.year c.nispline10##i.year"
local splines2 "c.nispline_avg1##i.year c.nispline_avg2##i.year c.nispline_avg3##i.year c.nispline_avg4##i.year c.nispline_avg5##i.year c.nispline_avg6##i.year c.nispline_avg7##i.year c.nispline_avg8##i.year c.nispline_avg9##i.year c.nispline_avg10##i.year"

local sr1 "i.netinc_2012cat##i.year i.netinc_2011cat##i.year i.valaddpw_ft_2012cat##i.year i.naics_2012##i.year i.year"
local sr2 "i.netinc_avgcat##i.year i.valaddpw_ft_2012cat##i.year i.naics_2012##i.year i.year"
local sr3 "i.netinc_2012cat20##i.year i.valaddpw_ft_2012cat##i.year i.naics_2012##i.year i.year"
local sr4 "i.netinc_avgcat20##i.year i.valaddpw_ft_2012cat##i.year i.naics_2012##i.year i.year"
local sr5 "`splines1' i.valaddpw_ft_2012cat##i.year i.naics_2012##i.year i.year"
local sr6 "`splines2' i.valaddpw_ft_2012cat##i.year i.naics_2012##i.year i.year"


*********
**  Main results
*********
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 4 5 6 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016), absorb(`sr`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A7_netincctrls.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}


*********
**  Placebo
*********

local t "ptop3"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 4 5 6 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016) & ptop==1 & t1_2012==0, absorb(`sr`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A7_placebo.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}




*************************************************************************
*************************************************************************
**  Robustness - control firms in high v. low exposed CZs  [Fig. A.10]
*************************************************************************
*************************************************************************

bys cz year: gen nfcz=_N
sum nfcz if year==2012,d
bys cz year: egen t1cz=sum(t1)
sum t1cz if year==2012,d
gen t1cz_share=t1cz/nfcz
sum t1cz_share if year==2012,d
sum t1cz_share if year==2012 & nfcz>=10,d

by cz year: gen nnn=_n==1
sum t1cz_share if year==2012 & nnn==1,d
sum t1cz_share if year==2012 & nfcz>=10 & nnn==1,d

foreach v in t1cz_share nfcz {
	gen zz=0
	replace zz = `v' if year==2012
	bys firm_tin: egen `v'_2012=sum(zz)
	drop zz
}

sum t1cz_share_2012 if year==2012,d
gen ctrt25 = 9
replace ctrt25 = 1 if t1cz_share_2012>=r(p75)
replace ctrt25 = 0 if t1cz_share_2012<=r(p25)
tab ctrt25
replace ctrt25 = . if ctrt25==9

gen ctrt20 = 9
replace ctrt20 = 1 if t1cz_share_2012>=.20
replace ctrt20 = 0 if t1cz_share_2012<=.137
tab ctrt20
replace ctrt20 = . if ctrt20==9

local r4 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year"

local t "ctrt25"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r'_p25: quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if t1_2012==0 & (ctrt25==0 | ctrt25==1), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}		
}

local t "ctrt20"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r'_p20: quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if t1_2012==0 & (ctrt20==0 | ctrt20==1), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}		
}
esttab using "$OUTPUT/figure_A10_ge.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
eststo clear






****************************
****************************
****************************
** Heterogeneity
	* Figure A.4
	* Figure A.9
	* Table A.10
	* Table E.16
****************************
****************************
****************************

*********************************************************************************************
*********************************************************************************************
**  By netinc deciles  [Figure A.4]
*********************************************************************************************
*********************************************************************************************

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"
local ddevents "`t' post dd"
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
		forval cc=1/10 {
			eststo va`cc'_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & netinc_2012cat==`cc' & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
		}
	}
	esttab using "$OUTPUT/figure_A4.csv", replace se ar2 mtitles( ) keep(dd) 
	eststo clear
}



*********************************************************************************************
*********************************************************************************************
**  Robust - loss firms analysis [Fig A.9, Table A.10]
*********************************************************************************************
*********************************************************************************************

************
*  Statistics of baseline loss firms [Table A.10]
************
gen loss_2012=netinc_2012<=0
gen loss = netinc<=0
tab year loss if loss_2012==1
tab year loss if loss_2012==0

gen vv=loss if year>2012
bys firm_tin: egen loss_post=sum(vv)
drop vv
gen loss1=loss_post>=1 & loss_post~=.
gen loss2=loss_post>=2 & loss_post~=.
gen loss3=loss_post>=3 & loss_post~=.
gen loss4=loss_post==4

preserve
	keep if loss_2012==1
	collapse (count) n=firm_tin (sum) loss (mean) loss_post loss1 loss2 loss3 loss4 (p50) loss_post_med=loss_post , by(year loss_2012)
	gen tt1112=99
	tempfile ta
	save `ta', replace
restore
preserve
	keep if loss_2012==1 & tt1112==1
	collapse (count) n=firm_tin (sum) loss (mean) loss_post loss1 loss2 loss3 loss4 (p50) loss_post_med=loss_post , by(year loss_2012 tt1112)
	append using `ta'
	sort tt1112 loss_2012 year
	gen sh=loss/n
	outsheet using $OUTPUT/table_A10.csv, comma replace
restore


************
*  Persistance in top bracket for loss firms? [Figure A.9]
************
* first stage tables
foreach tt in tt1112 {
preserve
	keep if `tt'==1
	collapse (count) n=firm_tin (sum) t1  ,by(year loss_2012)
	gen sh=t1/n
	gen trt=1
	gen tvc = "`tt'"
	tempfile te1_`tt'
	save `te1_`tt'', replace
restore
preserve
	keep if `tt'==0
	collapse (count) n=firm_tin (sum) t1  ,by(year loss_2012)
	gen sh=t1/n
	gen trt=0
	gen tvc = "`tt'"
	tempfile te0_`tt'
	save `te0_`tt'', replace
restore
}
preserve
	use `te1_tt1112', clear
	gen zz=1
	foreach tt in tt1112 {
		append using `te1_`tt''
		append using `te0_`tt''
	}
	drop if zz==1
	drop zz
	sort tvc loss_2012 trt year
	outsheet using $OUTPUT/figure_A9.csv, comma replace
restore




*********************************************************************************************
*********************************************************************************************
**  Robustness - states w/ and w/out tax changes - ncg  [Table E.16]
*********************************************************************************************
*********************************************************************************************

* variable for states with >=0.5pp changes in tax rate by 2016
gen chstate = 0
replace chstate = 1 if state_2012==27 | state_2012==33 | state_2012==51
*MN, NE then VT
replace chstate = 2 if state_2012==25 | state_2012==32  | state_2012==16  | state_2012==20  |  state_2012==39 | state_2012==18  | state_2012==31  | state_2012==15
*ME | ND | IA | KS then OH | IL | NC | HI
tab chstate if year==2012

* empl stats
bys year chstate: egen n_chstate=sum(n_ft_lower)
by year: egen n_fttot=sum(n_ft_lower)
gen zsh=n_chstate/n_fttot
tab zsh chstate if year==2012

* prep state MTR changes
* create share of exposure (actual/max)
gen sharemtr=dlnmtr_iv_2012/0.123481

* for each state, add or subtract relevant change in mtr from state tax change
gen mtr_state = mtr_2013
*MN
replace mtr_state = mtr_2013 + sharemtr*0.0235 if state_2012==27
*NE
replace mtr_state = mtr_2013 + sharemtr*0.015 if state_2012==33
*VT
replace mtr_state = mtr_2013 + sharemtr*0.0074 if state_2012==51

*ME
replace mtr_state = mtr_2013 - sharemtr*0.0135 if state_2012==25
*ND
replace mtr_state = mtr_2013 - sharemtr*0.0093 if state_2012==32
*IA
replace mtr_state = mtr_2013 - sharemtr*0.0083 if state_2012==16
*KS
replace mtr_state = mtr_2013 - sharemtr*0.0178 if state_2012==20
*OH
replace mtr_state = mtr_2013 - sharemtr*.0093 if state_2012==39
*IL
replace mtr_state = mtr_2013 - sharemtr*.0125 if state_2012==18
*NC
replace mtr_state = mtr_2013 - sharemtr*.02 if state_2012==31
*HI
replace mtr_state = mtr_2013 - sharemtr*.025 if state_2012==15

foreach p in mtr_state {
	gen ln`p'=ln(`p') if `p' > 0
	replace ln`p'=ln(`p'+((`p'^2)+1)^.5) if `p'<=0
}
gen dlnmtr_state = lnmtr_state - lnmtr
replace dlnmtr_state = 0 if dlnmtr_state<0

foreach n in dlnmtr_state {
	gen zz = 0
	replace zz = `n' if year==2012
	bys firm_tin: egen `n'_2012=sum(zz)
	drop zz
}

local la0 "none"
local la1 "incr"
local la2 "decr"

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
	* loop over state groups
	foreach st in 0 1 2 {
		eststo r`r'_`la`st'': quietly reghdfe `rlog'`o' `events'  if (year==2011 | year==2012 | year==2016) & chstate==`st' [aw=n_ft_lower_2012], absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/table_E16_itt.csv", replace se ar2 mtitles( ) keep(t2016) 
	eststo clear
}

* Elasticity
local trt "tt1112_ncg"
local t "dlnmtr_state_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
	* loop over state groups
	foreach st in 0 1 2 {
		eststo r`r'_`la`st'': quietly reghdfe `rlog'`o' `events'  if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016) & chstate==`st'  [aw=n_ft_lower_2012], absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	eststo r`r'_all: quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_E16_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) 
	eststo clear
}











clear
exit

